﻿/*
 * 设计模式 - 组合模式
 * 目的: 这就是一棵树
 */

using System.Text;

Node node = new Node()
{
    Name = "根目录",
    Children = new List<Node>()
    {
        new Node(){Name = "子节点A"},
        new Node()
        {
            Name = "子节点A-1",
            Children = new List<Node>()
            {
                new Node(){Name = "子节点A-1-1"}
            }
        }
    }
};

Console.WriteLine(node.ToString());
class Node
{
    public string Name { get; set; }

    public ICollection<Node> Children { get; set; } = new HashSet<Node>();

    public override string ToString()
    {
        var builder = new StringBuilder();
        builder.Append(Name);
        builder.AppendLine();
        Loop(builder, Children, 1);
        return builder.ToString();
    }

    public void Loop(StringBuilder builder, IEnumerable<Node> nodes, int deep)
    {
        foreach (var child in nodes)
        {
            builder.AppendFormat("|{0}{1}", new string('-', deep), child.Name);
            builder.AppendLine();
            deep++;
            Loop(builder, child.Children, deep);
        }
    }
}